package com.wang.controller;

import com.aaa.entity.UAddr;
import com.aaa.entity.UUserInfo;
import com.aaa.security.util.WebUtils;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wang.service.BBusinessInfoService;
import com.wang.service.PhoneService;
import com.wang.service.UAddrService;
import com.wang.service.UUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author :  DongZheng Wang
 * @PROJECT_NAME: fifth-cai-parent
 * @date :  2023-03-03 16:11
 **/

@RestController
@RequestMapping("/user")
public class EchoController {
    @Autowired
    private UUserInfoService uUserInfoService;
    @Autowired
    private BBusinessInfoService bBusinessInfoService;
    @Autowired
    private UAddrService uAddrService;


    @RequestMapping("/findUserById")
    public Result Echo() {
        Result echo = bBusinessInfoService.Echo();
        return new Result(2000, "Echo", echo);
    }

    @RequestMapping("/upload01")
    public Result updateResult(@RequestBody MultipartFile file) {
        Result update = bBusinessInfoService.update(file);
        System.out.println(update);
        return update;
    }

    @RequestMapping("/updateMessage")
    public Result updateMessage(@RequestBody UUserInfo userInfo) {
        boolean b = uUserInfoService.updateById(userInfo);
        return new Result(2000, "修改成功");
    }

    @RequestMapping("/addr/findAll")
    public Result address() {
        Result page = uAddrService.page();
        return new Result(2000, "success", page);
    }

    @RequestMapping("/addr/insertAddr")
    public Result insertAd(@RequestBody UAddr uAddr) {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = (String) tokenChaim.get("username");
        LambdaQueryWrapper<UUserInfo> wrapper = new LambdaQueryWrapper<UUserInfo>();
        wrapper.eq(UUserInfo::getTelephone, username);
        UUserInfo one = uUserInfoService.getOne(wrapper);
        uAddr.setUid(one.getId());
        uAddrService.save(uAddr);
        return new Result(2000, "success");
    }

    @RequestMapping("/addr/deleteById")
    public Result deteById(String id) {
        LambdaQueryWrapper<UAddr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UAddr::getId, id);
        uAddrService.remove(wrapper);
        return new Result(2000, "success");
    }

    @RequestMapping("/addr/updateById")
    public Result updateById(@RequestBody UAddr uAddr) {
        uAddrService.updateById(uAddr);
        return new Result(2000, "success");
    }

    @Autowired
    private PhoneService phoneService;

    @RequestMapping("/noteByPhone/{mobile}")
    public Result noteByPhone(@PathVariable String mobile) {
        Result phone = phoneService.phone(mobile);
        return new Result(2000, "success", phone);
    }

    @RequestMapping("/ls/updatePassword")
    public Result updatePassword( String mobile, String newPassword) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> chaim = JwtUtil.getTokenChaim(token);
        String username = (String) chaim.get("username");
        LambdaQueryWrapper<UUserInfo> wrapper = new LambdaQueryWrapper<UUserInfo>();
        wrapper.eq(UUserInfo::getTelephone, username);
        UUserInfo one = uUserInfoService.getOne(wrapper);
        if (one != null) {
            String encode = passwordEncoder.encode(newPassword);
            if (!passwordEncoder.matches(encode, one.getPassword())) {
                LambdaUpdateWrapper<UUserInfo> wrapper1 = new LambdaUpdateWrapper<UUserInfo>();
                wrapper1.eq(UUserInfo::getUname, one.getUname());
                wrapper1.set(UUserInfo::getTelephone,mobile);
                wrapper1.set(UUserInfo::getPassword, encode);
               uUserInfoService.update(wrapper1);
                return new Result(2000, "success");
            }
        }
        return new Result(5000, "修改密码有误");
    }
}
